<?php

    function checkUsernamePassword($u, $p) {
    	global $db_manager;
    	$xusers = new x_users($db_manager);
    	
    	return ($xusers->check_identity($u, $p));
    }

   function checkLogin(){
        // Check if user is logged in.
        $auth = isUserLoggedIn();
        if ($auth == false) {
            // User not logged yet.
            if (isset($_REQUEST['username']) == true) {
                // Request comes from login page, so check username and password
                if (checkUsernamePassword($_REQUEST['username'], $_REQUEST['password'])) {
                    $auth = true;
                    $_SESSION['username'] = trim($_REQUEST['username']);
                }
            }
        }
        
        return $auth;
    }    
    
    function checkAccessAllowed($page, $login_page){
    	
        if (!checkLogin()){
        	$page = $login_page;
        }
        
        return $page;
    }
    
    
    
    function isUserLoggedIn(){
    	
        return ( isset( $_SESSION['username'] ) );
    
    }

?>